package com.cqut.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;

import com.cqut.service.IEvaluationDetailsService;
import com.cqut.model.EvaluationDetails;
import com.cqut.dto.LimitShowDTO;
import com.cqut.util.ResponseUtil;
import com.cqut.dto.EvaluationDetailsDTO;

@RestController
@RequestMapping("/api/evaluationDetails")
public class EvaluationDetailsController {
	@Resource(name = "evaluationDetailsService")
	private IEvaluationDetailsService evaluationDetailsService;
	
	/**
	 * 版本预览
	 **/
	@RequestMapping(value = "/version", method = RequestMethod.GET)
	public ResponseEntity<String> versionPreview(@RequestParam("evaluationId") String evaluationId) {
		System.out.println(evaluationId);
		System.err.println("+++++++++++++++");
		//Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> versionList = evaluationDetailsService.getVersionList(evaluationId);

		System.out.println(versionList.toString());

		return ResponseUtil.getResEntityForGetAndJsonArray(versionList);
	}
	
	/**
	 * 得到评审指标表
	 * 
	 * @param evaluationName
	 * @param evaluationId
	 * @param limitShowDTO
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResponseEntity<String> evaluationIndexList(@RequestParam("evaluationId") String evaluationId,
			@RequestParam("evaluationName") String evaluationName, @RequestParam("parentId") String parentId,
			@Validated LimitShowDTO limitShowDTO, BindingResult errors) {
		if (errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		Map<String, Object> result = new HashMap<String, Object>();
		System.out.println(parentId);


		List<Map<String, Object>> evaluationDetailsList = evaluationDetailsService.getEvaluationList(parentId,
				evaluationId, evaluationName, limitShowDTO);
		long count = evaluationDetailsService.getEvaluationListCount(parentId, evaluationId, evaluationName);

		result.put("rows", evaluationDetailsList);
		result.put("total", count);

		return ResponseUtil.getResEntityForGetAndJson(result);
	}

	

	@RequestMapping(value = "/{targetId}", method = RequestMethod.GET)
	public ResponseEntity<String> show(@PathVariable("targetId") String targetId) {
		Map<String, Object> evaluationDetailsModel = evaluationDetailsService.findMap(targetId);

		return ResponseUtil.getResEntityForGetAndJson(evaluationDetailsModel);
	}

	/**
	 * 新增评审指标
	 **/
	@RequestMapping(value = "/addEvaluation", method = RequestMethod.POST)
	public ResponseEntity<String> create(@Validated EvaluationDetailsDTO evaluationDetailsDTO, BindingResult errors) {
		if (errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		EvaluationDetails evaluationDetailsModel = evaluationDetailsDTO.toModel();
		System.out.println("+++++++++++++++++5555555+++++++");

		return ResponseUtil.getResEntityForPPP(evaluationDetailsService.addEvaluationDeatils(evaluationDetailsModel));
	}

	/**
	 * 评审指标详情
	 **/
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	public ResponseEntity<String> detail(@RequestParam("targetId") String targetId) {
		return ResponseUtil.getResEntityForGetAndJson(evaluationDetailsService.getEvaluationDeatilsById(targetId));
	}

	/**
	 * 修改评审指标
	 **/
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseEntity<String> update(@Validated EvaluationDetailsDTO evaluationDetailsDTO, BindingResult errors) {
		if (errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		EvaluationDetails evaluationDetailsModel = evaluationDetailsDTO.toModel();

		return ResponseUtil.getResEntityForPPP(evaluationDetailsService.updateEvaluation(evaluationDetailsModel));
	}

	/**
	 * 删除一个评审指标
	 **/
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ResponseEntity<String> destory(@RequestParam("evaluationId") String evaluationId) {
		System.out.println(evaluationId);
		return ResponseUtil.getResEntityForDel(evaluationDetailsService.deleteOneEvaluation(evaluationId));
	}
	@RequestMapping(value = "/deletes", method = RequestMethod.POST)
	public ResponseEntity<String> deletes(@RequestParam("evaluationId") String evaluationId) {
		System.out.println(evaluationId);
		return ResponseUtil.getResEntityForDel(evaluationDetailsService.deleteWholeEvaluation(evaluationId));
	}

	@RequestMapping(value = "/{targetId}", method = RequestMethod.PUT)
	public ResponseEntity<String> update11(@PathVariable("targetId") String targetId,
			@Validated EvaluationDetailsDTO evaluationDetailsDTO, BindingResult errors) {

		EvaluationDetails evaluationDetailsModel = evaluationDetailsDTO.toModel();
		evaluationDetailsModel.setTargetId(targetId);
		return ResponseUtil.getResEntityForPPP(evaluationDetailsService.update(evaluationDetailsModel));
	}

	@RequestMapping(value = "/{targetId}", method = RequestMethod.DELETE)
	public ResponseEntity<String> destory11(@PathVariable("targetId") String targetId) {
		return ResponseUtil.getResEntityForDel(evaluationDetailsService.delete(targetId));
	}

	/*@RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
	public ResponseEntity<String> deletes(@RequestParam("primaryKeys") String[] primaryKeys) {
		return ResponseUtil.getResEntityForDel(evaluationDetailsService.batchDelete(primaryKeys));
	}*/

	/**
	 * 版本使用情况
	 **/
	@RequestMapping(value = "/usageList", method = RequestMethod.GET)
	public ResponseEntity<String> usage(@RequestParam("reviewVersionId") String reviewVersionId,
			@RequestParam("competitionName") String competitionName, @Validated LimitShowDTO limitShowDTO,
			BindingResult errors) {
		if (errors.hasErrors())
			return ResponseUtil.getResponseEntityWhenInvalidReqParams();

		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> processDetailList = evaluationDetailsService.getUsageList(reviewVersionId,
				competitionName, limitShowDTO);
		long count = evaluationDetailsService.getUsageListCount(reviewVersionId, competitionName);
		System.out.println(processDetailList.toString());
		result.put("rows", processDetailList);
		result.put("total", count);

		return ResponseUtil.getResEntityForGetAndJson(result);
	}

}
